package com.luych.toolbox.common.enums;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public enum DataSourceType {

    MYSQL("MySQL", "mysql", Cmd.MYSQL_EXPORT, Cmd.MYSQL_IMPORT),
    DM("达梦", "dm", Cmd.DM_EXPORT, Cmd.DM_IMPORT);

    private final String value;
    private final String driver;
    private final Cmd exportCmd;
    private final Cmd importCmd;

    DataSourceType(String value, String driver, Cmd exportCmd, Cmd importCmd) {
        this.value = value;
        this.driver = driver;
        this.exportCmd = exportCmd;
        this.importCmd = importCmd;
    }

    public String getValue() {
        return value;
    }

    public String getDriver() {
        return driver;
    }

    public Cmd getExportCmd(String host, int port, String user, String pass, String db, String file) {
        return new Cmd(
                exportCmd.getCmd().replace("${host}", host)
                        .replace("${port}", String.valueOf(port))
                        .replace("${user}", user)
                        .replace("${pass}", pass)
                        .replace("${db}", db)
                        .replace("${file}", file),
                exportCmd.getCodes()
        );
    }

    public Cmd getImportCmd(String host, int port, String user, String pass, String db, String file) {
        return new Cmd(
                importCmd.getCmd().replace("${host}", host)
                        .replace("${port}", String.valueOf(port))
                        .replace("${user}", user)
                        .replace("${pass}", pass)
                        .replace("${db}", db)
                        .replace("${file}", file),
                importCmd.getCodes()
        );
    }

    public static class Cmd {
        private static final String CMD_MYSQL_EXPORT = "mysqldump --set-gtid-purged=OFF --no-tablespaces --complete-insert --skip-extended-insert --skip-add-locks -h${host} -P${port} -u${user} -p${pass} ${db} > ${file}";
        private static final String CMD_MYSQL_IMPORT = "mysql -h${host} -P${port} -u${user} -p${pass} ${db} < ${file}";
        private static final String CMD_DM_EXPORT = "dexp '${user}/\"${pass}\"@${host}:${port}' DUMMY=Y NOLOG=Y NOLOGFILE=Y SCHEMAS=${db} DIRECTORY=. FILE=${file}";
        private static final String CMD_DM_IMPORT = "dimp '${user}/\"${pass}\"@${host}:${port}' DUMMY=Y NOLOG=Y NOLOGFILE=Y SCHEMAS=${user} DIRECTORY=. FILE=${file} TABLE_EXISTS_ACTION=REPLACE";

        private static final List<Integer> CODE_MYSQL_EXPORT = Collections.singletonList(0);
        private static final List<Integer> CODE_MYSQL_IMPORT = Collections.singletonList(0);
        private static final List<Integer> CODE_DM_EXPORT = Arrays.asList(0, 2);
        private static final List<Integer> CODE_DM_IMPORT = Arrays.asList(0, 2);

        public static Cmd MYSQL_EXPORT = new Cmd(CMD_MYSQL_EXPORT, CODE_MYSQL_EXPORT);
        public static Cmd MYSQL_IMPORT = new Cmd(CMD_MYSQL_IMPORT, CODE_MYSQL_IMPORT);
        public static Cmd DM_EXPORT = new Cmd(CMD_DM_EXPORT, CODE_DM_EXPORT);
        public static Cmd DM_IMPORT = new Cmd(CMD_DM_IMPORT, CODE_DM_IMPORT);

        private final String cmd;
        private final List<Integer> codes;

        public Cmd(String cmd, List<Integer> codes) {
            this.cmd = cmd;
            this.codes = codes;
        }

        public String getCmd() {
            return cmd;
        }

        public List<Integer> getCodes() {
            return codes;
        }
    }
}
